const express = require("express")
const router = express.Router();
const passport = require("passport")
const mongoose = require('mongoose')

// 引入Profie
const Profile = require("../../models/Profile");



router.post('/add', passport.authenticate("jwt", { session: false }), (req, res) => {
    const pro = {};
    if (req.body.title) pro.title = req.body.title;
    if (req.body.content) pro.content = req.body.content;
    if (req.body.class) pro.class = req.body.class;
    if (req.user._id) pro.author_id = req.user._id;
    if (req.body.cid) pro.cid = req.body.cid;
    new Profile(pro).save()
        .then(Profile => {
            res.json(Profile)
            console.log(Profile);
        })
})

//GET api/profile 获取数据
router.get('/', async (req, res) => {
    // const category = await Profile.find().populate('User')
    // res.json(category)
    const category = await Profile.aggregate([{
        $lookup: {
            from: "users",
            localField: "author_id",
            foreignField: "_id",
            as: "user"
        }
    }])
    res.json(category)
})
// 模糊查询  文章 
router.get('/cata/:title', async (req, res) => {
    // Profile.find({ $or: [{ title: { $regex: req.params.title } }, { content: { $regex: req.params.title } }] }).then(data => {
    //     res.json(data)
    // })
    const category = await Profile.aggregate([{
        $lookup: {
            from: "users",
            localField: "author_id",
            foreignField: "_id",
            as: "user"
        },
    }, {
        $match: {
            $or: [{ title: { $regex: req.params.title } }]
        }
    }])
    res.json(category)
})
//GET api/profil：author_id 获取 获得某个用户的文章
router.get('/user/', async (req, res) => {
    console.log(req.query.id);
    const categ = await Profile.aggregate([{
        $lookup: {
            from: "articlecate",
            localField: "cid",
            foreignField: "_id",
            as: "cate"
        },
    }, {
        $match: {
            author_id: mongoose.Types.ObjectId(req.query.id)
        }

    }])
    res.json(categ)
})
// 搜索文章
router.get('/:title', (req, res) => {
    // console.log(req.user)
    Profile.find({ title: req.params.title }).then(Profile => {
        if (Profile) {
            res.json(Profile)
        } else {
            res.status(404).json('没有内容')
        }
    })
})
//GET api/profil/edit 获取 某一条数据
router.get('/edit/:id', (req, res) => {

    Profile.find({ _id: req.params.id }).then(Profile => res.json(Profile))
})
router.post('/edit/:id', (req, res) => {
    console.log(req.params.id);
    const pro = {};
    if (req.body.title) pro.title = req.body.title;
    if (req.body.content) pro.content = req.body.content;
    if (req.body.class) pro.class = req.body.class;
    Profile.findOneAndUpdate({ _id: req.params.id }, { $set: pro }, { new: true }).then(Profile => res.json(Profile))
})
//DELETE api/profil/delete 删除某一条数据
router.delete('/delete/:id', passport.authenticate("jwt", { session: false }), (req, res) => {
    Profile.findOneAndDelete({ _id: req.params.id }, function (err, Profile) {
        if (!err) {
            console.log("删除成功");
            res.status(200).json('ok')

        }
        console.log(err);

    })
})

module.exports = router;